﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace rpxnow_library.conf
{
    /// <summary>
    /// This class reads the configuration keys from App.config.
    /// </summary>
    public sealed class ApplicationConfiguration : IConfigurationSectionHandler
    {
        /// <summary>
        /// Static constructor for the object.
        /// </summary>
        static ApplicationConfiguration()
        {
            Object obj = ConfigurationManager.GetSection("rpxnowlogindemo");
        }

        #region IConfigurationSectionHandler Members

        object IConfigurationSectionHandler.Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            NameValueCollection settings;
            try
            {
                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            }
            catch
            {
                settings = null;
            }

            if (settings != null)
            {
                // Read Application settings from configuration file.
                ApplicationConfiguration.connectionStringName = ApplicationConfiguration.ReadSetting(settings, CONNECTION_STRING_NAME, "");
                ApplicationConfiguration.rpxFrameUrl = ApplicationConfiguration.ReadSetting(settings, RPX_FRAME_URL, "");
                ApplicationConfiguration.siteTitle = ApplicationConfiguration.ReadSetting(settings, SITE_TITLE, "");
                ApplicationConfiguration.metaDescription = ApplicationConfiguration.ReadSetting(settings, META_DESCRIPTION, "");
                ApplicationConfiguration.metaKeywords = ApplicationConfiguration.ReadSetting(settings, META_KEYWORDS, "");
            }
            return null;


        }

        #endregion

        /// <summary>
        /// Reads a setting from a given NameValueCollection, and sets
        /// default value if the key is not available in the collection.
        /// </summary>
        /// <param name="settings">The settings collection from which the keys
        /// are to be read.</param>
        /// <param name="key">Key name to be read.</param>
        /// <param name="defaultValue">Default value for the key.</param>
        /// <returns>Actual value from settings, or defaultValue if settings
        /// does not have this key.</returns>
        private static string ReadSetting(NameValueCollection settings, string key, string defaultValue)
        {
            if (settings == null || key == null)
                throw new ArgumentNullException();

            try
            {
                Object setting = settings[key];
                return (setting == null) ? defaultValue : (string)setting;
            }
            catch
            {
                return defaultValue;
            }
        }

        #region Public Properties

        public static string ConnectionStringName
        {
            get
            {
                return ApplicationConfiguration.connectionStringName;
            }
        }

        public static string RpxFrameUrl
        {
            get
            {
                return ApplicationConfiguration.rpxFrameUrl;
            }
        }

        public static string SiteTitle
        {
            get
            {
                return ApplicationConfiguration.siteTitle;
            }
        }

        public static string MetaDescription
        {
            get
            {
                return ApplicationConfiguration.metaDescription;
            }
        }

        public static string MetaKeywords
        {
            get
            {
                return ApplicationConfiguration.metaKeywords;
            }
        }

        #endregion

        #region Private Property Members

        private static string connectionStringName;
        private static string rpxFrameUrl;
        private static string siteTitle;
        private static string metaDescription;
        private static string metaKeywords;

        #endregion

        #region Setting Constants

        private const string CONNECTION_STRING_NAME = "ConnectionStringName";
        private const string RPX_FRAME_URL = "RpxFrameUrl";      
        private const string SITE_TITLE = "SiteTitle";
        private const string META_DESCRIPTION = "MetaDescription";
        private const string META_KEYWORDS = "MetaKeywords";  

        #endregion
    }
}
